package my.diplom.util;

import my.diplom.entity.Host;
import my.diplom.entity.Resource;
import my.diplom.dao.service.HostService;

import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.LinkedHashMap;

import org.apache.log4j.Logger;

/**
 * Created by IntelliJ IDEA.
 * User: gb
 * Date: 03.11.2009
 * Time: 2:29:48
 * To change this template use File | Settings | File Templates.
 */
public class HostUtil {

    private static final Logger log = Logger.getLogger(HostUtil.class);

    public static Map<Long, Integer[]> getUsedPercent(List<Host> hosts){
        LinkedHashMap<Long, Integer[]> percentMap = new LinkedHashMap<Long, Integer[]>();
        for(Host h: hosts){
            Float cpuSum = new Float(0);
            Float ramSum = new Float(0);
            Float hddSum = new Float(0);
            for(Resource r: h.getResources()){
                cpuSum += r.getCpu();
                ramSum +=r.getRam();
                hddSum +=r.getHdd();
            }

            Integer[] percentArray = new Integer[3];
            percentArray[0] = (int)((100 * cpuSum)/h.getCpu());
            percentArray[1] = (int)((100 * ramSum)/h.getRam());
            percentArray[2] = (int)((100 * hddSum)/h.getHdd());
            percentMap.put(h.getHostId(), percentArray);
        }
        return percentMap;
    }

    public static void setUsage(List<Host> hosts){
        for(Host h: hosts){
            Float cpuSum = new Float(0);
            Float ramSum = new Float(0);
            Float hddSum = new Float(0);

            for(Resource r: h.getResources()){
                cpuSum += r.getCpu();
                ramSum +=r.getRam();
                hddSum +=r.getHdd();
            }

            Integer[] percentArray = new Integer[3];
            h.setCpuUsage((int)((100 * cpuSum)/h.getCpu()));
            h.setRamUsage((int)((100 * ramSum)/h.getRam()));
            h.setHddUsage((int)((100 * hddSum)/h.getHdd()));

            log.debug("For host" + h.getHostName());
            log.debug("Cpu usage" + h.getCpuUsage());
            log.debug("Ram Usage" + h.getRamUsage());
            log.debug("Hdd Usage" + h.getHddUsage());
            
        }
    }

    public static float getFreeCpu(Host host){
        float cpuUsage = 0;
        for (Resource r: host.getResources()){
            cpuUsage += r.getCpu();
        }
        return host.getCpu() - cpuUsage;
    }

     public static float getFreeRam(Host host){
        float ramUsage = 0;
        for (Resource r: host.getResources()){
            ramUsage += r.getRam();
        }
        return host.getRam() - ramUsage;
    }

     public static float getFreeHdd(Host host){
        float hddUsage = 0;
        for (Resource r: host.getResources()){
            hddUsage += r.getHdd();
        }
        return host.getHdd() - hddUsage;
    }

    public static boolean isValidResource(Host host, Resource resource){
        if (getFreeCpu(host) > resource.getCpu() &&
            getFreeHdd(host) > resource.getHdd() &&
            getFreeRam(host) > resource.getRam() ) return true;
        return false;

    }

}
